/*
* Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved.
* Description: none
* Author: s00452708
* Create: 2018-08-01
* Notes: none
* History: 2018-08-01 s00452708 create
*/

#ifndef __CHAR_TYPE_H__
#define __CHAR_TYPE_H__


#define TYPE_SP 0x80
#define TYPE_X  0x40
#define TYPE_S  0x20
#define TYPE_P  0x10
#define TYPE_C  0x08
#define TYPE_D  0x04
#define TYPE_L  0x02
#define TYPE_U  0x01

#define ASCII_CTRL_ESC         (0x1b) /* Escape ascii code */
#define ASCII_CTRL_ARROW       (0x5b) /* Arrow ascii code */
#define ASCII_CTRL_ARROW_LEFT  (0x44) /* Arrow left ascii code */
#define ASCII_CTRL_ARROW_RIGHT (0x43) /* Arrow right ascii code */
#define ASCII_CTRL_ARROW_UP    (0x41) /* Arrow up ascii code */
#define ASCII_CTRL_ARROW_DOWN  (0x42) /* Arrow down ascii code */
#define ASCII_CTRL_DEL         (0x7f) /* Delete ascii code */

extern const unsigned char g_ctype[]; /* _ctype[] fast lookup table */

#define __ismask(x) (g_ctype[(int)(unsigned char)(x)]) /* is mask bit set */

#define isalnum(c)  ((__ismask(c) & (TYPE_U | TYPE_L | TYPE_D)) != 0)            /* c is alpha or num */
#define isalpha(c)  ((__ismask(c) & (TYPE_U | TYPE_L)) != 0)                 /* c is num */
#define iscntrl(c)  ((__ismask(c) & (TYPE_C)) != 0)                      /* c is control code */
#define isdigit(c)  ((__ismask(c) & (TYPE_D)) != 0)                      /* c is digit */
#define isgraph(c)  ((__ismask(c) & (TYPE_P | TYPE_U | TYPE_L | TYPE_D)) != 0)       /* c is graphic char */
#define islower(c)  ((__ismask(c) & (TYPE_L)) != 0)                      /* c is lower case */
#define isprint(c)  ((__ismask(c) & (TYPE_P | TYPE_U | TYPE_L | TYPE_D | TYPE_SP)) != 0) /* c is printable char */
#define ispunct(c)  ((__ismask(c) & (TYPE_P)) != 0)                      /* c is punctuated char */
#define isspace(c)  ((__ismask(c) & (TYPE_S)) != 0)                      /* c is space */
#define isupper(c)  ((__ismask(c) & (TYPE_U)) != 0)                      /* c is upper case */
#define isxdigit(c) ((__ismask(c) & (TYPE_D | TYPE_X)) != 0)                 /* c is hex digit */

#define isblank(c) (c == ' ' || c == '\t') /* c is blank */

#define isascii(c) (((unsigned char)(c)) <= 0x7f) /* c is ascii */
#define toascii(c) (((unsigned char)(c)) & 0x7f)  /* convert c to ascii */

static inline unsigned char __tolower(unsigned char c) /* convert c from upper to lower case */
{
    if (isupper(c)) {
        c += 'a' - 'A';
    }
    return c;
}

static inline unsigned char __toupper(unsigned char c) /* convert c from lower to upper case */
{
    if (islower(c)) {
        c -= 'a' - 'A';
    }
    return c;
}

#define tolower(c) __tolower(c)
#define toupper(c) __toupper(c)

#endif /* __CHAR_TYPE_H__ */
